package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.Setmeal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"套餐管理接口"})
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    SetmealService setmealService;
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO dto){
        log.info("分页查询套餐{}",dto);
        PageResult pageResult = setmealService.pageQuery(dto);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = {"setmealCache"}, key = "#dto.categoryId")
    public Result addSetMeal(@RequestBody SetmealDTO dto){
        log.info("新增{}套餐",dto);
        setmealService.addSetmeal(dto);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = {"setmealCache"},allEntries = true)
    public Result deleteAll(@RequestParam List<Long> ids){
        log.info("删除id为{}的套餐",ids);
        setmealService.deleteAll(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("id查询")
    public Result<Setmeal> querySetMealById(@PathVariable Long id){
        log.info("查询Id为{}的套餐",id);
        Setmeal setmeal = setmealService.querySetMealById(id);
        return Result.success(setmeal);
    }

    @PutMapping
    @ApiOperation("更新套餐")
    @CacheEvict(cacheNames = {"setmealCache"},key = "#setmealDTO.categoryId")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("更新套餐状态")
    @CacheEvict(cacheNames = {"setmealCache"},allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status,Long id){
        setmealService.updateSetmealStatus(status,id);
        return Result.success();
    }
}
